#include "remote.h"
#include "regedit.h"
#include <SimpleSerialShell.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

const char* ssid = "H3C_Wisdom";
const char* password = "Wisdom:5721";

const char* PARAM_MESSAGE = "message";

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta name="referrer" content="no-referrer">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>富华硬件组专用小空调</title>
    <style>
        @font-face {
         font-family: 'led regular';
         src: url('https://gitee.com/wandeng/wd_html_file/raw/master/font/ds_digital/DS-DIGI.TTF');
        }

        *{
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        body{
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: rgb(241, 241, 241);
        }
        .container{
            position: relative;
            width: 400px;
            height: 500px;
           /*  border: 1px solid rgb(201, 201, 201); */
        }
        .air{
            position: relative;
            margin: 0 auto;
            width: 300px;
            height: 120px;
            background-color: rgb(255, 255, 255);
            border-radius: 10px 10px 0 0;
            filter: drop-shadow(0 2px 4px rgb(145, 145, 145));
        }
        .air::after{
            content: '';
            position: absolute;
            bottom: -21px;
            left: 0;
            height: 20px;
            width: 300px;
            background-color: rgb(255, 255, 255);
            border-radius: 0 0 20px 20px;
        }
        .picture{
            position: absolute;
            top: 10px;
            left: 10px;
            width: 40px;
            height: 65px;
            border-radius: 5px;
        }
        .pattern{
            position: absolute;
            top: 25px;
            right: 35px;
            width: 20px;
            height: 20px;
            opacity: 0;
            transition: all 0.3s;
        }
        .font{
            /* 引用LED字体 */
            font-family: "led regular";
            position: absolute;
            right: 30px;
            top: 45px;
            width: 50px;
            height: 30px;
            line-height: 30px;
            font-size: 25px;
            color: rgb(184, 184, 184);
            opacity: 0;
            transition: all 0.3s;
        }
        .logo{
            position: absolute;
            left: 50%;
            bottom: 3px;
            transform: translateX(-50%);
            font-size: 10px;
            color: rgb(139, 139, 139);
        }
        .dot{
            position: absolute;
            bottom: -8px;
            right: 12px;
            width: 5px;
            height: 5px;
            border-radius: 50%;
            background-color: rgb(19, 221, 53);
            z-index: 1;
            opacity: 0;
            transition: all 0.3s;
        }
        .wind{
            position: relative;
            top: 30px;
            width: 250px;
            height: 60px;
            margin: 0 auto;
            opacity: 0;
            transition: all 1.2s;
            animation: move 1.5s ease-in-out infinite;
        }
        @keyframes move{
            0%,100%{
                transform: translateY(0px);
            }
            50%{
                transform: translateY(5px);
            }
        }
        .wind>span{
            position: absolute;
            width: 5px;
            height: 40px;
            background-color: rgb(206, 206, 206);
 
        }
        .wind1{           
            left: 20%;           
            transform: rotate(20deg);
        }
        .wind2{           
            left: 50%;           
        }
        .wind3{           
            left: 80%;           
            transform: rotate(-20deg);
        }
        .btn{
            position: relative;
            top: 50px;
            margin:0 auto;
            width: 100px;
            height: 150px;
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;
            align-items: center;
        }
        .btn>div{
            width: 35px;
            height: 35px;
            border-radius: 50%;
            font-size: 12px;
            line-height: 35px;
            text-align: center;
            background-color: rgb(204, 204, 204);
            box-shadow: 0 2px 4px rgb(151, 151, 151);
            cursor: pointer;
            user-select: none;
        }
        .btn>div:active{
            background-color: rgb(240, 240, 240);
        }
        .sun,.snow,.open{
            background-size: 50% 50%;
            background-position: 50% 50%;
            background-repeat: no-repeat;
        }
        .btn .sun{
            background-image: url(https://gitee.com/wandeng/wd_html_file/raw/master/img/air/sun.png);
            background-color: rgb(255, 130, 13);
        }
        .btn .snow{
            background-image: url(https://gitee.com/wandeng/wd_html_file/raw/master/img/air/snow.png);
            background-color: rgb(13, 138, 255);
        }
        .btn .open{
            background-image: url(https://gitee.com/wandeng/wd_html_file/raw/master/img/air/open.png);
            background-color: rgb(33, 255, 13);
        }
    </style>

    <script type="text/javascript">
        function send_air_data() {
    
          var send_data = "air 0 ";
          send_data += temp+" "+air_sta+" 1"+" "+num;
    
          var xhr = new XMLHttpRequest();
          xhr.open("GET", "/air_set?value=" + send_data, true);
          xhr.send();
        }
    </script>
</head>
<body>
    <!-- 底层盒子 -->
    <div class="container">
        <div class="air">
            <img src="https://gitee.com/wandeng/wd_html_file/raw/master/img/air/air-conditioning.png" alt="x" class="picture">
            <img class="pattern" src="https://gitee.com/wandeng/wd_html_file/raw/master/img/air/snow.png" alt="x">
            <span class="font">17°C</span>
            <span class="logo">☁</span>
            <span class="dot"></span>
        </div>
        <div class="wind">
            <span class="wind1"></span>
            <span class="wind2"></span>
            <span class="wind3"></span>
        </div>
        <div class="btn">
            <div class="up">▲</div>
            <div class="down">▼</div> 
            <div class="sun"></div>
            <div class="snow"></div>
            <div class="open"></div>
        </div>
    </div>
    <audio src="https://gitee.com/wandeng/wd_html_file/raw/master/img/air/open.mp3" class="music" loop></audio>
    <audio src="https://gitee.com/wandeng/wd_html_file/raw/master/img/air/12763.wav" class="ding"></audio>
    <script>
        var open = document.querySelector(".open");
        var sun = document.querySelector(".sun");
        var snow = document.querySelector(".snow");
        var up = document.querySelector(".up");
        var down = document.querySelector(".down");
        var pattern = document.querySelector(".pattern");
        var font = document.querySelector(".font");
        var dot = document.querySelector(".dot");
        var wind = document.querySelector(".wind");
        var music = document.querySelector(".music");
        var ding = document.querySelector(".ding");
        var temp = 2;
        var num = 17;
        var air_sta = 1;

        open.addEventListener('click',function(){
            if(temp==1){
                open.style.backgroundColor = 'rgb(33, 255, 13)';
                pattern.style.opacity = 0;
                font.style.opacity = 0;
                dot.style.opacity = 0;
                wind.style.opacity = 0;               
                temp=2;
                music.pause();
            }else if(temp==2){
                open.style.backgroundColor = 'red';
                pattern.style.opacity = 1;
                font.style.opacity = 1;
                dot.style.opacity = 1;
                wind.style.opacity = 1;
                temp=1;
                music.play();
            }
      
            send_air_data();
        })
        up.addEventListener('click',function(){
            if(num==30) return ;
            num++;
            font.innerHTML = num+"°C";
            ding.load();
            ding.play();
      
            send_air_data();
        })
        down.addEventListener('click',function(){
            if(num==16) return ;
            num--;
            font.innerHTML = num+"°C";
            ding.load();
            ding.play();
      
            send_air_data();
        })
        sun.addEventListener('click',function(){
            pattern.src = "https://gitee.com/wandeng/wd_html_file/raw/master/img/air/sun.png";
            ding.play();
            music.load();
            music.play();
            air_sta = 2;
      
            send_air_data();
            
        })
        snow.addEventListener('click',function(){
            pattern.src = "https://gitee.com/wandeng/wd_html_file/raw/master/img/air/snow.png";
            ding.play();
            music.load();
            music.play();
            air_sta = 1;
      
            send_air_data();
      
        })
        
    </script>
</body>
</html>)rawliteral";

char *GetParameters[16] = {0};
signed char prvGetNumberOfParameters( const char * pcCommandString )
{
    int8_t cParameters = 0;

  GetParameters[cParameters] = (char *)pcCommandString;
      
  while( *pcCommandString != 0x00 )
    {
        if( ( *pcCommandString ) == ' ' )
        {
            cParameters++;
            GetParameters[cParameters] = (char *)pcCommandString+1;
        }

        pcCommandString++;
    }
  
    return cParameters;
}

uint8_t moduleControl(uint8_t port, uint8_t modact ,uint8_t *modpara) {
  uint8_t ret = 0;

  if(port < 10) {
    if(modact == 0) { //空调控制
      if(((modpara[0] == 1)||(modpara[0] == 2))
              &&((modpara[1] == 1)||(modpara[1] == 2)||(modpara[1] == 4))
              &&((modpara[2] >= 1)&&(modpara[2] <= 4))
              &&((modpara[3] >= 15)&&(modpara[3] <= 32))
        ) {
        if(RemoteDev[port].Type == TYPE_AIRCON) ret = 1;
      }
    } else if(modact == 1) { //电视即机顶盒
      if((modpara[0] >= 1)&&(modpara[0] <= 40)) {
        if(RemoteDev[port].Type == TYPE_TVBOX) ret = 1;
      }
    } else if(modact == 2) { //小帅投影仪
      if((modpara[0] >= 1)&&(modpara[0] <= 12)) {
        if(RemoteDev[port].Type == TYPE_OTHER) ret = 1;
      }
    }
  }

  if(ret == 1) {
    RemoteDev[port].Package(modpara);
  }

  return ret;
}

int set_ir(int argc, char **argv)
{
    uint8_t port=0;
    uint8_t para[10]={0};
    uint8_t result;

    if(strcmp(argv[1], "air") == 0) {    //
      port = atoi(argv[2]);
      para[0] = atoi(argv[3]);
      para[1] = atoi(argv[4]);
      para[2] = atoi(argv[5]);
      para[3] = atoi(argv[6]);
      
      moduleControl(port, 0, para);
    } else if(strcmp(argv[1], "config") == 0) {    //
      port = atoi(argv[2]);
      para[0] = atoi(argv[3]);
      
      RemoteSelect(port,para[0]);
    }
    return EXIT_SUCCESS;
}

void Config_Callback(AsyncWebServerRequest *request)
{
  uint8_t para[10]={0};
  if (request->hasParam("value")) // 如果有值下发
  {
    String HTTP_Payload = request->getParam("value")->value();    // 获取下发的数据
    Serial.printf("[%lu]%s\r\n", millis(), HTTP_Payload.c_str()); // 打印调试信息
    prvGetNumberOfParameters(HTTP_Payload.c_str());
    
    para[0] = atoi(GetParameters[2]);
    para[1] = atoi(GetParameters[3]);
    para[2] = atoi(GetParameters[4]);
    para[3] = atoi(GetParameters[5]);

    moduleControl(0, 0, para);
  }
  request->send(200, "text/plain", "OK"); // 发送接收成功标志符
}

void setup()
{
  Serial.begin(115200);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
      Serial.printf("WiFi Failed!\n");
      return;
  }
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
            { request->send_P(200, "text/html", index_html); });
  server.on("/air_set", HTTP_GET, Config_Callback);
  server.begin();  // 初始化HTTP服务器

  shell.attach(Serial);
  shell.addCommand(F("ir para1 para2 para3 para4 para5 para6"), set_ir);
  RemoteInit();

  RemoteSelect(0,11);
  RemoteSelect(1,40);
}

void loop()
{
   shell.executeIfInput();
   delay(10);
}
